package com.dxx.D201910312055.auto;

import android.accessibilityservice.AccessibilityService;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.view.accessibility.AccessibilityEvent;
import android.view.accessibility.AccessibilityNodeInfo;
import android.widget.Toast;

import com.dxx.D201910312055.dxx.AutoAbstract;
import com.dxx.D201910312055.dxx.utils.FindAccessibilityNodeInfosByClassName;


import java.util.List;

import static com.dxx.D201910312055.dxx.AutoService.mService;
import static java.lang.System.out;

public class Auto_Brush_the_treasure extends AutoAbstract {
    //    属于自己的脚本是否开启
    private static boolean isAction = false;


    public static String[] ClassNameData = {"", "", ""};


    public void onStart() {
        isAction = true;
//        mService.performGlobalAction(AccessibilityService.GLOBAL_ACTION_HOME);

        if (checkPackInfo("com.jm.video")) {
            PackageManager packageManager = mService.getPackageManager();
            Intent intent = packageManager.getLaunchIntentForPackage("com.jm.video");
            mService.startActivity(intent);
        } else {
            isAction = false;
            Toast.makeText(mService, "脚本失败,指定app不存在", Toast.LENGTH_SHORT).show();
        }


    }

    //        在运行的
    public void onRun(AccessibilityEvent event) {


//虽然外界无法访问，但是这里可以操作同样的效果
        if (isAction) {
            List<AccessibilityNodeInfo> accessibilityNodeInfoList = null;
            AccessibilityNodeInfo accessibilityNodeInfo = null;


            //解决悬浮窗,必须开始就解决
            try {
                accessibilityNodeInfoList = mService.getRootInActiveWindow().findAccessibilityNodeInfosByViewId("com.jm.video:id/cancel");
                if (!accessibilityNodeInfoList.isEmpty()) {
                    accessibilityNodeInfoList.get(0).performAction(AccessibilityNodeInfo.ACTION_CLICK);
                    accessibilityNodeInfoList = null;
                }
            } catch (Exception e) {

            }


            //解决加载
            try {
                accessibilityNodeInfoList = mService.getRootInActiveWindow().findAccessibilityNodeInfosByText("正在加载...");
//             isStart=mService.getRootInActiveWindow().findAccessibilityNodeInfosByViewId("com.jm.video:id/textFoot").isEmpty();
                if (!accessibilityNodeInfoList.isEmpty()) {
                    mService.performGlobalAction(AccessibilityService.GLOBAL_ACTION_BACK);
                    mService.performGlobalAction(AccessibilityService.GLOBAL_ACTION_BACK);
//                这种睡眠能让其它线程执行
                    isAction = false;
                    try {
                        Thread.sleep(3000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    isAction = true;


                    PackageManager packageManager = mService.getPackageManager();
                    Intent intent = packageManager.getLaunchIntentForPackage("com.jm.video");
                    mService.startActivity(intent);
                    accessibilityNodeInfoList = null;
                }
            } catch (Exception e) {

            }

//            下面错误可能中断
            if ((int) event.getEventType() == 4) {
//            mPerformAction(mService.getRootInActiveWindow(), "android.support.v7.widget.RecyclerView", AccessibilityNodeInfo.ACTION_SCROLL_FORWARD);
//
//            mPerformAction(mService.getRootInActiveWindow(), "android.support.v4.view.ViewPager", AccessibilityNodeInfo.ACTION_SCROLL_FORWARD);
//            mPerformAction(mService.getRootInActiveWindow(), "android.support.v4.view.ViewPager", AccessibilityNodeInfo.ACTION_SCROLL_BACKWARD);

                accessibilityNodeInfo = new FindAccessibilityNodeInfosByClassName().frontFindAccessibilityNodeInfosByClassName(mService.getRootInActiveWindow(), ClassNameData[0], 2);
                if (accessibilityNodeInfo != null) {
                    accessibilityNodeInfo.performAction(AccessibilityNodeInfo.ACTION_SCROLL_FORWARD);
                }


                accessibilityNodeInfo = new FindAccessibilityNodeInfosByClassName().frontFindAccessibilityNodeInfosByClassName(mService.getRootInActiveWindow(), ClassNameData[1], 0);
                if (accessibilityNodeInfo != null) {
                    accessibilityNodeInfo.performAction(AccessibilityNodeInfo.ACTION_SCROLL_FORWARD);
                }
                accessibilityNodeInfo = new FindAccessibilityNodeInfosByClassName().frontFindAccessibilityNodeInfosByClassName(mService.getRootInActiveWindow(), ClassNameData[2], 0);
                if (accessibilityNodeInfo != null) {
                    accessibilityNodeInfo.performAction(AccessibilityNodeInfo.ACTION_SCROLL_BACKWARD);
                }
            }


        }
    }


    @Override
    public void onStop() {
        isAction = false;
    }


}

